import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaTools, WeaErrorPage, WeaTable, WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools;
import EHeader from '../common/EHeader';
import EContent from './EContent';
import FooterMore from "../common/FooterMore";
import { NoRightCom, handleHeight } from '../../util/common';
//元素组件
class ElementTable extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            edata: null,
            refresh: false,
        }
        this.handleEData = this.handleEData.bind(this);
        this.handleRefresh = this.handleRefresh.bind(this);
        this.handleDelete = this.handleDelete.bind(this);
    }
    handleDelete(){
        this.setState({
            edata: null,
        });
        ls.set('edata-'+this.props.config.params.eid, null);
    }
    handleRefresh(){
        this.setState({
            refresh: true,
        });
        this.handleEData(this.props.config.params);
    }
    handleEData(params){
        const { eid, api } = params;
        WeaTools.callApi(api || '/api/portal/element/rss', 'POST', {...params}).then(result=> {
            ls.set('edata-'+eid, result);
            window.elements_more_params[eid] = {isRouteUrl: true, tabid: result.currenttab || ''};
            this.setState({
                edata: result,
                refresh: false,
            });
       });
    }
    componentDidMount(){
        const { config = {} } = this.props;
        const { params = {} } = config;
        const { eid } = params;
        const edata = ls.getJSONObj(`edata-${eid}`);
        if (config.islsdata && edata) {
            this.setState({ edata });
        } else {
            this.handleEData(params);
        }
    }
    componentWillReceiveProps(nextProps){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = { 
            item: nextProps.config.item, 
            params: nextProps.config.params, 
            isHasRight: nextProps.config.isHasRight,
        };
        if(!isEqual(config, _config) || nextProps.config.isRender){
            this.handleEData(nextProps.config.params);
        }
    }
    shouldComponentUpdate(nextProps, nextState){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = { 
            item: nextProps.config.item, 
            params: nextProps.config.params, 
            isHasRight: nextProps.config.isHasRight,
        };
        return !isEqual(this.state, nextState) || !isEqual(config, _config)
    }
    render() {
        const { edata, refresh } = this.state;
        const { config, fontSizeMp } = this.props;
        if(!config || !edata) return <div/>;
        const { isHasRight, item } = config;
        const { eid, ebaseid, content, header, contentview, style } = item;
        const { style: style1= {} } = contentview;
        let EContentHtml = <div/>;
        if(isHasRight){
            if(!isEmpty(edata)) EContentHtml = <EContent config={config} handleDelete={this.handleDelete} handleRefresh={this.handleRefresh} edata={edata}/>;
        }else{
            EContentHtml = <NoRightCom/>
        }

        return <div className="item"
                    style={style}
                    id={`item_${eid}`} 
                    data-eid={eid} data-ebaseid={ebaseid} 
                    data-needRefresh={item.needRefresh} 
                    data-cornerTop={item.cornerTop} 
                    data-cornerTopRadian={item.cornerTopRadian}
                    data-cornerBottom={item.cornerBottom} 
                    data-cornerBottomRadian={item.cornerBottomRadian}>
                    <EHeader config={config} handleRefresh={this.handleRefresh} handleDelete={this.handleDelete} fontSizeMp={fontSizeMp}/>
                    <div className="setting" id={`setting_${eid}`}></div>
                    <div className="content" id={`content_${eid}`} style={{width:'auto',_width:'100%'}}>
                        <div className="content_view" id={`content_view_id_${eid}`} style={{height: style1.height}}>
                            <Spin spinning={refresh}>{EContentHtml}</Spin>
                        </div>
                        <div style = {{textAlign:'right'}} id={`footer_${eid}`}><FooterMore config={this.props.config}/></div>
                    </div>
            </div>;
    }
}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage msg = { hasErrorMsg ? this.props.error :getLabel(388017,"对不起，元素加载异常，请联系管理员！") }/>
        );
    }
}
ElementTable = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(ElementTable);
export default ElementTable;

class TableList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            edata: null,
            refresh: false,
        }
    }
    componentDidMount(){
    }
    componentWillReceiveProps(nextProps){

    }
    shouldComponentUpdate(nextProps, nextState){
        return true;
    }
    render() {
        return <WeaTable columns = {formatData(tabdata.data[0], esetting)} showHeader={false} pagination={false} dataSource={tabdata.data} size="small"/>;
    }
}
//处理antd Table数据，返回columns
const formatData = (obj, esetting) => {
    const { imgSymbol, linkmode, isremind, widths, titles, ebaseid, eid } = esetting;
    const columns = new Array;
    var i = 0;
    //加载行小图标 
    if (imgSymbol) {
        const iObj = {
            dataIndex: 'imgSymbol',
            key: 'imgSymbol',
            width: '8px',
            render: () => <img style={{marginBottom:'4px'}} name='esymbol' src={imgSymbol}/>
        }
        columns.push(iObj);
    }
    for (var k in obj) {
        var colObj = new Object;
        const kv = obj[k];
        if (kv && typeof kv === 'object' && kv.constructor === Object) {       //判断属性值的类型，Object类型
            colObj = {
                dataIndex: k,
                className: ebaseid === ELEMENT_TYPES.WORKFLOW && k === 'requestname' ? 'reqdetail':'',
                key: k,
                render: (text) => loadRemind(text, linkmode, isremind)
            }
        } else if (kv && typeof kv === 'object' && kv.constructor === Array) { //判断属性值的类型，List类型，建模查询中心元素用到                   
            colObj = {
                dataIndex: k,
                key: k,
                render: (text) => loadListName(text, linkmode)
            }
        } else {                                                         //判断属性值的类型，字符串类型 
            colObj = {
                dataIndex: k,
                key: k,
                render: (text) => {
                    if(typeof text === 'string'){
                        if (_isHtml(text)) {                                 //判断字符串是否含有html 
                            return <font className={text.indexOf("class='font'") == -1 ? 'font' : ''} dangerouslySetInnerHTML={{__html:text}}></font>
                        } else {
                            return <font className="font">{text}</font>
                        }
                    }else{
                        return <font className="font"></font>
                    }

                }
            }
        }
    }
    return columns;
}



